package ltesim.editors.initPage.listener;

import java.io.IOException;

import ltesim.editors.LEditorPage;
import ltesim.xmlParser.XMLParser;
import ltesim.xmlParser.xmlData.XMLDataset;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

/**
 * Listener class for adapting save button action
 * 
 * @author xirka
 * 
 */
public class SaveButtonListener implements SelectionListener {
	LEditorPage page;

	public SaveButtonListener(LEditorPage _page) {
		page = _page;
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		// System.out.println("saveButtonListener:widgetSelected");
		XMLDataset dataset = page.getXMLData();
		// System.out.println(dataset);
		XMLParser parser = page.getXMLParser();
		parser.setXMLData(dataset);
		try {
			parser.writeXML();
		} catch (IOException ex) {
			System.err.println("Error in writing XML");
			ex.printStackTrace();
		}
		page.setDirty(false);
		page.fireDirtyStateChange();
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// System.out.println("saveButtonListener:widgetDefaultSelected");
		widgetSelected(e);
	}

}